/*
 * author: Andriamananony, Meß, Falk
 */

package start;

import java.util.ArrayList;
import java.util.Map.Entry;

import plan.Plan;
import schedule.EDF;
import schedule.EDF2;
import schedule.EDF4;
import schedule.IterateOrdersThreshold;
import schedule.WEDF;
import util.JSONParser;
import util.TimeLog;
import auftrag.Auftrag;
import auftrag.Produkt;
import auftrag.Ressource;
import constraints.AllOrdersPlanned;
import constraints.DueDate;
import constraints.IConstraint;
import constraints.NoMachineOverload;
import constraints.OperationOrder;

public class Start {
	public static long time = 115000;
	public static void main(String[] args) {
		long t1 = System.currentTimeMillis();
		long t2 = 0;
		JSONParser parser = new JSONParser(args[0]);

		Plan p = new Plan();
		String bestMethod = "";
		int bestIterationMethod = 1;
		int bestQuality = Integer.MAX_VALUE;
		for(int i = 1; i<3;i++){
			
			for(Ressource r : parser.getRessources())
				r.clear();
			
			t2 = System.currentTimeMillis();
			Start.time = time-(t2-t1);  
			
			t1 = System.currentTimeMillis();
			p = new EDF().makePlan(parser.getOrders(), parser.getRessources(), i);
			if(p.getQuality(true) < bestQuality){
				bestIterationMethod = i;
				bestMethod = "EDF";
				bestQuality = p.getQuality(false);
				if(p.getQuality(false) <= 0)
					break;
			}
			t2 = System.currentTimeMillis();
			Start.time = time-(t2-t1);
			
			if(Start.time <= (t2-t1))
				break;
			
			t1 = System.currentTimeMillis();
			for(Ressource r : parser.getRessources())
					r.clear();
			
			p = new EDF2().makePlan(parser.getOrders(), parser.getRessources(), i);
			if(p.getQuality(true) < bestQuality){
				bestIterationMethod = i;
				bestMethod = "EDF2";
				bestQuality = p.getQuality(false);
				if(p.getQuality(false) <= 0)
					break;
			}
			t2 = System.currentTimeMillis();
			Start.time = time-(t2-t1);
			
			if(Start.time <= (t2-t1))
				break;
			
			t1 = System.currentTimeMillis();
			for(Ressource r : parser.getRessources())
					r.clear();
			
			p = new EDF4().makePlan(parser.getOrders(), parser.getRessources(), i);
			if(p.getQuality(true) < bestQuality){
				bestIterationMethod = i;
				bestMethod = "EDF4";
				bestQuality = p.getQuality(false);
				if(p.getQuality(false) <= 0)
					break;
			}
			t2 = System.currentTimeMillis();
			Start.time = time-(t2-t1);
			
			if(Start.time <= (t2-t1))
				break;
			
			t1 = System.currentTimeMillis();
			for(Ressource r : parser.getRessources())
					r.clear();
			
			p = new WEDF().makePlan(parser.getOrders(), parser.getRessources(), i);
			if(p.getQuality(true) < bestQuality){
				bestIterationMethod = i;
				bestMethod = "WEDF";
				bestQuality = p.getQuality(false);
				if(p.getQuality(false) <= 0)
					break;
			}
			t2 = System.currentTimeMillis();
			Start.time = time-(t2-t1);
		}
		
		ArrayList<Auftrag> list;
		if(time > 2*(t2-t1) && bestQuality > 0){
			for(Ressource r : parser.getRessources())
				r.clear();
			
			if(bestMethod.equals("EDF")){
				list = new EDF().sortAufträge(parser.getOrders());
			} else if(bestMethod.equals("EDF2")){
				list = new EDF2().sortAufträge(parser.getOrders());
			} else if(bestMethod.equals("EDF4")){
				list = new EDF4().sortAufträge(parser.getOrders());
			} else {
				list = new WEDF().sortAufträge(parser.getOrders());
			} 
			
			for(Ressource r : parser.getRessources())
				r.clear();
			
			IterateOrdersThreshold it = new IterateOrdersThreshold();
			it.setMaxTime((long) (time*0.85));
			p = it.makePlan(list, parser.getRessources(), bestIterationMethod);
		}
		p.print();
	}
}
